<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">消耗统计</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-u-sm-12 am-form ">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.year">
                  <option value="">年份</option>
                  <option value="2017">2017</option>
                  <option value="2018">2018</option>
                  <option value="2019">2019</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.areaTeamId" :options="areaTeams">
                  <option value="">区域</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.periodId" :options="periods">
                  <option value="">期数</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.busTeamId" :options="busTeams">
                  <option value="">业务组</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.productId" :options="products">
                  <option value="">产品</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.gradeId" :options="grades">
                  <option value="">年级</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.subjectId" :options="subjects">
                  <option value="">科目</option>
                </select2>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" v-model="query.courseName" placeholder="请输入课程名称"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text"  v-model="query.className" placeholder="请输入班级名称"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text"  v-model="query.campusName" placeholder="请输校区"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <date-picker v-model="query.startDate" >
                  <input type="text" placeholder="请选择开始日期" data-am-datepicker readonly required >
                </date-picker>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <date-picker v-model="query.endDate" >
                  <input type="text" placeholder="请选择结束日期" data-am-datepicker readonly required >
                </date-picker>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-6 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="search" ><span class="am-icon-search"></span>查询
                </button>
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="exportExcel" v-if="hasPermission('export_consume_aggregation_list')"><span class="am-icon-download"></span>导出excel
                </button>
              </div>
            </div>


          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                label="统计开始日期"
                min-width="150">
                <template scope="scope">
                  {{scope.row.startDate | formatDate}}
                </template>
              </el-table-column>
              <el-table-column
                label="统计结束日期"
                min-width="150">
                <template scope="scope">
                  {{scope.row.endDate | formatDate}}
                </template>
              </el-table-column>
              <el-table-column
                prop="year"
                label="年份"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="season"
                label="季节"
                min-width="100">
              </el-table-column>

              <el-table-column
                prop="studentName"
                label="学生姓名"
                min-width="100">
              </el-table-column>

              <el-table-column
                prop="studentNo"
                label="学生编号"
                min-width="150">
              </el-table-column>
              <el-table-column
                prop="studentGradeName"
                label="就读年级"
                min-width="100">
              </el-table-column>

              <el-table-column
                prop="campusName"
                label="报读校区"
                min-width="200">
              </el-table-column>

              <el-table-column
                prop="className"
                label="报读班级"
                min-width="200">
              </el-table-column>
              <el-table-column
                prop="gradeName"
                label="年级"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="subjectName"
                label="科目"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="consumeClassTimeOfThis"
                label="消耗课时数"
                min-width="100">
              </el-table-column>
              <el-table-column
                label="消耗金额"
                min-width="100">
                <template scope="scope">
                  {{scope.row.consumeAmountOfThis | formatNumber(2)}}
                </template>
              </el-table-column>
              <el-table-column
                label="红冲消耗"
                min-width="100">
                <template scope="scope">
                  {{scope.row.redAmountOfThis | formatNumber(2)}}
                </template>
              </el-table-column>
              <el-table-column
                label="差额消耗"
                min-width="100">
                <template scope="scope">
                  {{scope.row.differenceAmountOfThis | formatNumber(2)}}
                </template>
              </el-table-column>

            </el-table>
          </div>
          <div class="am-u-lg-12 am-cf">

            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="handlePage"/>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import io from '../../lib/io'
  import Pagination from '../base/Pagination.vue'
  import util from '../../lib/util'
  import moment from 'moment'

  export default{
    data: function () {
      return {
        tableData: [],
        allData: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        query: {
          areaTeamId : '',
          busTeamId : '',
          productId : '',
          startDate : util.formatDate( util.firstDayOfMonth() ),
          endDate : util.formatDate( util.endDayOfMonth() ),
          accessToken: '',
          year: '2018',
          className: '',
          courseName: '',
          subjectId: '',
          gradeId: '',
          campusName: '',
          periodId: ''
        },
        products:[],
        periods:[],
      }
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
      this.loadTableData(this.pageNo)
      this.loadProductData()
      this.loadPeriodData()
    },
    components: {Pagination},
    computed: {
      areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
          .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
          })
        return options
      },
      busTeams: function () {
        var options = ( ( this.query.areaTeamId  ) ? ( this.$root.config.groupBusTeams[this.query.areaTeamId] || [] ) : [] )
          .map(function (item) {
            return {value: item.busTeamId, text: item.name}
          })
        this.query.busTeamId = ''
        return options
      },
      grades: function () {
        return this.$root.config.grades.map(function(item){
          return {value: item.gradeId, text: item.gradeName}
        })
      },
      subjects: function () {
        return this.$root.config.subjects.map(function(item){
          return {value: item.subjectId, text: item.subjectName}
        })
      }
    },
    watch: {
      'query.year' (newVal, oldVal) {
          this.loadPeriodData()
      },
      'query.areaTeamId' (newVal, oldVal) {
          this.loadPeriodData()
      }
    },
    methods: {
      loadPeriodData: function () {
        var _this = this
        io.post(io.periodByYearAndAreaTeamId, {
          areaTeamId : this.query.areaTeamId,
          year: this.query.year ? moment(this.query.year).format('YYYY'): '',
        }, function (ret) {
          if (ret.success) {
            _this.periods = ret.data.map(function (item) {
              return {value: item.periodId, text: item.periodName }
            })
            if(!_this.periods.length){
              _this.$alert('当前没有设置期数')
              _this.query.periodId = ''
            }else{
              let curPeriodId = ret.data.filter(item => item.isCurrent == 1 )
              if(curPeriodId.length){
                _this.query.periodId = curPeriodId[0].periodId
              }else{
                _this.query.periodId = ''
              }
              
            }
            
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      search: function () {
        this.loadTableData(1)
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        _this.$showLoading()
        io.post(io.apiAdminReportAggregateConsumeList, _this.query, function (ret) {
          _this.$hiddenLoading()
          if (ret.success) {
            _this.total = ret.data.length
            _this.allData = ret.data
            _this.handlePage(pageNo)
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      handlePage(pageNo) {
        this.pageNo = pageNo || this.pageNo || 1
        this.tableData = this.allData.slice((this.pageNo-1)*this.pageSize,this.pageSize*(this.pageNo))
      },
      loadProductData: function () {
        var _this = this
        io.post(io.apiAdminBaseProductList, {}, function (ret) {
          if (ret.success) {
            _this.products = ret.data.map(function (item) {
              return {value: item.productId, text: item.name}
            })
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      loadCourseData: function () {
        var _this = this
        io.post(io.apiAdminBaseCourseList, {}, function (ret) {
          if (ret.success) {
            _this.courses = ret.data.map(function (item) {
              return {value: item.courseTemplateId, text: item.courseName}
            })
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      exportExcel:function(){
        io.downloadFile(io.apiAdminReportExportConsumeAggregation,this.query)
      }
    }
  }
</script>
